package com.example.zhihu.service.Imp;

import com.example.zhihu.mapper.*;
import com.example.zhihu.pojo.Answer;
import com.example.zhihu.pojo.Comments;
import com.example.zhihu.pojo.UserLikeAnswer;
import com.example.zhihu.pojo.vo.AnswerVO;
import com.example.zhihu.pojo.vo.CommentVO;
import com.example.zhihu.pojo.vo.QuestionVO;
import com.example.zhihu.service.AnswerService;
import com.example.zhihu.utils.TimeAgoUtils;
import com.github.pagehelper.PageHelper;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AnswerServiceImp implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerCustomMapper answerCustomMapper;

    @Autowired
    private UserLikeAnswerMapper userLikeAnswerMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentCustomMapper commentCustomMapper;

    @Autowired
    private QuestionCustomMapper questionCustomMapper;


    @Autowired
    private Sid sid;

    @Override
    public Answer addAnswer(Answer answer) {
        answer.setId(sid.nextShort());
        answer.setLikeCount(0);
        answer.setCreateTime(new Date());
        answer.setStatus(1);
        answerMapper.insert(answer);
        return answer;
    }

    @Override
    public void delAnswer(String id) {
        Answer answer = new Answer();
        answer.setId(id);
        answer.setStatus(0);
        answerMapper.updateByPrimaryKeySelective(answer);
    }

    @Override
    public List<AnswerVO> answerList(String userId, String questionId, String desc) {
        List<AnswerVO> alist = answerCustomMapper.answerList(userId, questionId, desc);
        return alist;
    }

    @Override
    public void updateAnswer(Answer answer) {
        Example example = new Example(Answer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", answer.getId());
        answerMapper.updateByExampleSelective(answer, example);
    }

    @Override
    public Integer showNum(String qid) {
        Integer num = answerMapper.showAnswerCount(qid);
        return num;
    }

    @Override
    public void userLikeAnswer(String userId, String answerId, String creatorId, Integer status) {
        String likeId = sid.nextShort();
        UserLikeAnswer userLikeAnswer = new UserLikeAnswer();
        userLikeAnswer.setId(likeId);
        userLikeAnswer.setCreateTime(new Date());
        userLikeAnswer.setAnswerId(answerId);
        if (status == 0){
            userLikeAnswer.setIsLike(1);
        } else if(status == 1){
            userLikeAnswer.setIsCollect(1);
        }
        userLikeAnswer.setUserId(userId);
        userLikeAnswerMapper.insert(userLikeAnswer);

        // 回答喜欢数量增加
        answerCustomMapper.addAnswerLikeCount(answerId);

        // 用户受喜欢数增加
        userMapper.addReceiveLikeCount(creatorId);
    }

    @Override
    public void userUnlikeAnswer(String userId, String answerId, String creatorId) {
        Example example = new Example(UserLikeAnswer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("answerId",answerId);
        userLikeAnswerMapper.deleteByExample(example);

        // 回答喜欢数量减少
        answerCustomMapper.reduceAnswerLikeCount(answerId);

        // 用户受喜欢数减少
        userMapper.reduceReceiveLikeCount(creatorId);
    }

    @Override
    public void addComment(Comments comment) {
        comment.setId(sid.nextShort());
        comment.setCreateTime(new Date());
        commentMapper.insert(comment);
    }

    @Override
    public List<CommentVO> getComments(String answerId) {
        List<CommentVO> list = commentCustomMapper.queryComments(answerId);
        for(CommentVO c : list) {
            String timeAgo = TimeAgoUtils.format(c.getCreateTime());
            c.setTimeAgoStr(timeAgo);
        }
        return list;
    }

    @Override
    public AnswerVO queryAnswer(String answerId) {
        AnswerVO answerVO = answerCustomMapper.queryAnswer(answerId);
        return answerVO;
    }

    @Override
    public Integer showCommentCount(String answerId) {
        Integer commentCount = commentCustomMapper.queryCommentCount(answerId);
        return commentCount;
    }

    @Override
    public QuestionVO queryOneQuestion(String questionId) {
        QuestionVO qvo = questionCustomMapper.queryOneQuestion(questionId);
        return qvo;
    }

    @Override
    public List<AnswerVO> hotAnswerList() {
        List<AnswerVO> list =answerCustomMapper.hotAnswerList();
        return list;
    }

}
